package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.service.impl.SetMealServiceImpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: QingYingYan
 * @Date: 2025-10-22 9:06
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetMealController {
    @Autowired
    SetMealService setMealService;
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐：ids = {}", ids);
        setMealService.delete(ids);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result getPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult page = setMealService.getPage(setmealPageQueryDTO);
        return Result.success(page);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    public Result startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        log.info("套餐起售停售: status={},id={}", status, id);
        setMealService.startOrStop(status, id);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setMeal",key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐");
        setMealService.addMeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐: id={}", id);
        SetmealVO setmealVO = setMealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐: setmealDTO={}", setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }


}
